package com.csi.controller.back;

import cn.hutool.jwt.JWTUtil;
import com.csi.domain.Audit;
import com.csi.domain.Result;
import com.csi.service.AuditService;
import com.csi.utils.ReturnResults;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Map;

@RequestMapping("/audit")
@RestController
public class AuditController {

    @Autowired
    private AuditService auditService;

    /**
     * 更新审核状态
     * @param audit 审核对象
     * @return 执行结果
     */
    @PostMapping("/updateHotel")
    public Object updateAuditHotel(@RequestBody Audit audit, HttpServletRequest request){
        audit.setAuditId(Long.parseLong(JWTUtil.parseToken(request.getHeader("token")).getPayload("employeeId").toString()));
        audit.setAuditTime(new Timestamp(System.currentTimeMillis()));
        int updated = auditService.updateAuditHotel(audit);
        if (updated >0){
            return new Result("执行成功", ReturnResults.ACCESS_SUCCESS);
        }
        return new Result("执行失败", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 更新加盟审核状态
     * @param audit
     * @param request
     * @return
     */
    @PostMapping("/updateFranchise")
    public Object updateAuditFranchise(@RequestBody Audit audit,HttpServletRequest request){
        audit.setAuditId(Long.parseLong(JWTUtil.parseToken(request.getHeader("token")).getPayload("employeeId").toString()));
        audit.setAuditTime(new Timestamp(System.currentTimeMillis()));
        int updated = auditService.updateAuditFranchise(audit);
        if (updated >0){
            return new Result("执行成功", ReturnResults.ACCESS_SUCCESS);
        }
        return new Result("执行失败", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 更新房型审核状态
     * @param audit
     * @param request
     * @return
     */
    @PostMapping("/updateAuditRoomType")
    public Object updateAuditRoomType(@RequestBody Audit audit,HttpServletRequest request){
        audit.setAuditId(Long.parseLong(JWTUtil.parseToken(request.getHeader("token")).getPayload("employeeId").toString()));
        audit.setAuditTime(new Timestamp(System.currentTimeMillis()));
        int updated = auditService.updateAuditRoomType(audit);
        if (updated >0){
            return new Result("执行成功", ReturnResults.ACCESS_SUCCESS);
        }
        return new Result("执行失败", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 查看审核酒店信息
     * @param auditStatus
     * @return
     */
    @GetMapping("/findHotel")
    public Object findAuditHotel(@RequestParam(name = "auditStatus",required = false) String auditStatus){
        Map<String,Object> auditInfo = auditService.findAuditHotel(auditStatus);
        if (auditInfo != null){
            return new Result("执行成功", ReturnResults.ACCESS_SUCCESS,auditInfo.values());
        }
        return new Result("执行失败", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 根据id查看酒店审核信息
     * @param id
     * @return
     */
    @GetMapping("/findHotelId")
    public Object findAuditHotelById(@RequestParam(name = "id") long id){
        Map<String,Object> auditInfoId = auditService.findAuditHotelId(id);
        if (auditInfoId != null){
            return new Result("执行成功", ReturnResults.ACCESS_SUCCESS,auditInfoId);
        }
        return new Result("执行失败", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 查询酒店加盟审核信息
     * @return
     */
    @GetMapping("findFranchise")
    public Object findAuditFranchise(@RequestParam(name = "auditStatus",required = false) String auditStatus){
        Map<String,Object> auditInfo = auditService.findAuditFranchise(auditStatus);
        if (auditInfo != null){
            return new Result("执行成功", ReturnResults.ACCESS_SUCCESS,auditInfo.values());
        }
        return new Result("执行失败", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 根据id查找酒店加盟审核信息
     * @param franchiseId
     * @return
     */
    @GetMapping("/findFranchiseId")
    public Object findAuditFranchiseId(@RequestParam(name = "id") long franchiseId){
        Map<String,Object> auditInfoId = auditService.findAuditFranchiseId(franchiseId);
        if (auditInfoId != null){
            return new Result("执行成功", ReturnResults.ACCESS_SUCCESS,auditInfoId);
        }
        return new Result("执行失败", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 查询酒店房型审核信息
     * @param auditStatus
     * @param peopleNum
     * @return
     */
    @GetMapping("/findRoomType")
    public Object findRoomType(@RequestParam(name = "auditStatus",required = false) String auditStatus,@RequestParam(name = "peopleNum",required = false,defaultValue = "0") Integer peopleNum){
        if (auditStatus == null) {
            auditStatus = "";  // 或者你可以设置为特定的默认值
        }
        Map<String,Object> auditInfo = auditService.findRoomType(auditStatus,peopleNum);
        if (auditInfo != null){
            return new Result("执行成功", ReturnResults.ACCESS_SUCCESS,auditInfo.values());
        }
        return new Result("执行失败", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 根据房型id查询酒店审核信息
     * @param roomTypeId
     * @return
     */
    @GetMapping("/findRoomTypeId")
    public Object findRoomTypeId(@RequestParam("id") long roomTypeId){
        Map<String,Object> auditInfoId = auditService.findRoomTypeId(roomTypeId);
        if (auditInfoId != null){
            return new Result("执行成功", ReturnResults.ACCESS_SUCCESS,auditInfoId);
        }
        return new Result("执行失败", ReturnResults.ACCESS_FAILED);
    }
}
